from django.http import HttpResponse, Http404
from nameremoved.extra.models import Extra
from nameremoved.helpers import render_with_requestcontext

def children(parent_id):
    return Extra.objects.order_by('title').filter(parent=parent_id)

def index(request):
    family = []
    for parent in Extra.tops.all().select_related():
        children = parent.extra_set.all().order_by('title')
        family.append((parent, children))
    c = {'family': family}
    return render_with_requestcontext('extra/all.html',c,request)

def single(request, which_main):
    try:
        main = Extra.tops.get(pagename=which_main)
    except Extra.DoesNotExist:
        raise Http404
    context = {'main': main,
         'children': Extra.bottoms.filter(parent=main.id),
    }
    return render_with_requestcontext('extra/single.html',context,request)

def double(request, which_main, which_second):
    try:
        child = Extra.bottoms.get(pagename=which_second)
    except Extra.DoesNotExist:
        raise Http404
    if child.parent.pagename != which_main: raise Http404
    context = {'main': child,
        'parent': child.parent,
        'siblings': child.parent.extra_set.all().order_by('title'),
    }
    return render_with_requestcontext('extra/double.html',context,request)
